#!/usr/bin/env python
# -*- conding:utf-8 -*-
# __Author__:'liudong'
import sys
import os
import pickle

LOGIN_USER = {'is_login': False}  # 判断用户登录成功与否
USER_TYPE = {'is_admin': False}  # 判断用户类型


# user_base_info = {'username': 'rain',
#              'card': card_num,
#              'password': commons.md5('8888'),
#              'credit': 15000,  # 信用卡额度
#              'balance': 15000,  # 本月可用额度
#              'saving': 0,  # 储蓄金额
#              "enroll_date": "2016-01-01",  # 注册时间
#              'expire_date': "2021-01-01",
#              'status': 0,  # 0=normal,1=locked,2=disabled
#              "debt": [],  # 欠款记录，如:[{'date':"2015_4_10","total}]
#              }
# pickle.dump(user_base_info,open('..\\db\\user.db','wb'))
def admin_login(user, passwd):
    user_password = []  # 保存用户、密码列表
    user_list = []
    with open('..\\db\\admins.db') as admin_file:
        for u in admin_file:  # 取出用户名和密码
            user_password = u.split()
            user_list.append(user_password[0])  # 保存用户名到user_list
            user_list.append(user_password[1])  # 保存密码到user_list
            # print(user_password)
            for user_login in user_list:  # 判断用户输入的用户名存在？
                passwd_index = user_list.index(user) + 1  # 取出用户名下标
                # print(passwd_index)
                if passwd == user_list[passwd_index]:  # 判断用户名对应的密码
                    print('You login as administrator>>>%s<<<' % user)
                    LOGIN_USER['is_login'] = True
                    USER_TYPE['is_admin'] = True
                    return True
                else:
                    print('Invalid username or password')
                    return False


def user_login(user,passwd):
    """
    普通用户登录程序
    :param user:
    :param passwd:
    :return:
    """

    user_list_file = open('..\\db\\user.db','rb')       #读取用户基本信息
    user_list = pickle.load(user_list_file)
    # print(user_list,type(user_list))
    # for user_loop in user_list:
    #     # print(user_list[user][1])
    while True:
        if user in user_list and passwd == user_list[user][1] :
            if user_list[user][7] == '1':
                print('your account is locked, please contact administrator!')
            else:
                print('Welcome user %s' %user)
                return True
        else:
            print('Invalid username or password!')
            return False
            continue
